<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="菜品名称" prop="fname">
      <el-input v-model="ruleForm.fname"></el-input>
    </el-form-item>


<!--    <el-form-item label="菜品种类" :prop="ruleForm.cid">-->

      <el-form-item label="菜品种类" prop="cid">
      <el-select v-model="ruleForm.cid" placeholder="请选择">
        <el-option
          v-for="item in depss" :key="item.cid"
          :label="item.cname" :value="item.cid">
        </el-option>
      </el-select>

    </el-form-item>


    <el-form-item label="菜品价格" prop="price">
      <el-input v-model="ruleForm.price"></el-input>
    </el-form-item>f
    <el-form-item label="菜品描述" prop="fdesc">
      <el-input type="textarea" v-model="ruleForm.fdesc"></el-input>
    </el-form-item>
    <el-form-item label="菜品图片">
      <el-upload
        class="avatar-uploader"
        action="/api/foodUpload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl"  class="avatar" v-model="ruleForm.image">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import axios from "axios";

  export default {
    name: "FoodAdd",
    data() {
      return {
        ruleForm: {
          fname: '',
          cid: '',
          fdesc: '',
          price: '',
          // sid:'',
          image: false,


        },
        imageUrl:'',
        depss: [],//下拉菜单

        rules: {
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post("/api/insertFood", this.ruleForm).then(res => {
              if (res.data == "1") {
                this.$router.push("/foodsList")
              } else {
                alert("新增失败")
              }
            })
          } else {
            console.log('错误的提交');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.ruleForm.image=res;
        alert(this.imageUrl)
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt10M = file.size / 1024 / 1024 < 10;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt10M) {
          this.$message.error('上传头像图片大小不能超过 10MB!');
        }
        return isJPG && isLt10M;
      },
      categoryInfo(){
        let data={};
        axios.get("/api/selectAll").then(res=>{
          if(res.data){
            this.depss=res.data;
          }else{
            console.log(1);
          }
        }).catch(function (error) {
          this.$message.error('系统错误');

        })

      },
      loadWater: function () { //加载列表
      },
    },

    mounted() {
      this.categoryInfo();

    }
  }
</script>

<style scoped>
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
  }

  .avatar-uploader :hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
